package com.rboat.web.controller.blog;

import com.rboat.blog.domain.BlogArticleType;
import com.rboat.blog.service.IBlogArticleTypeService;
import com.rboat.common.annotation.Log;
import com.rboat.common.core.controller.BaseController;
import com.rboat.common.core.domain.AjaxResult;
import com.rboat.common.core.domain.Ztree;
import com.rboat.common.core.page.TableDataInfo;
import com.rboat.common.enums.BusinessType;
import com.rboat.common.utils.poi.ExcelUtil;
import com.rboat.system.domain.SysDept;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章类型Controller
 * 
 * @author rboat
 * @date 2019-11-05
 */
@Controller
@RequestMapping("/blog/type")
public class BlogArticleTypeController extends BaseController
{
    private String prefix = "blog/type";

    @Autowired
    private IBlogArticleTypeService blogArticleTypeService;

    @RequiresPermissions("blog:type:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/type";
    }

    /**
     * 查询文章类型列表
     */
    @RequiresPermissions("blog:type:list")
    @PostMapping("/list")
    @ResponseBody
    public List<BlogArticleType> list(BlogArticleType blogArticleType)
    {
        List<BlogArticleType> list = blogArticleTypeService.selectBlogArticleTypeList(blogArticleType);
        return list;
    }
    /**
     * 选择上级tree
     */
    @GetMapping("/selectTypeTree/{id}")
    public String list(@PathVariable("id") String id, ModelMap modelMap)
    {
        modelMap.put("type",blogArticleTypeService.selectBlogArticleTypeById(id));
        return prefix+"/tree";
    }
    /**
     * 加载类型表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Map<String,Object>> treeData()
    {
        Map<String,Object> param=new HashMap<>();
        List<Map<String,Object>> ztrees = blogArticleTypeService.selectTypeTree(param);
        return ztrees;
    }

    /**
     * 导出文章类型列表
     */
    @RequiresPermissions("blog:type:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BlogArticleType blogArticleType)
    {
        List<BlogArticleType> list = blogArticleTypeService.selectBlogArticleTypeList(blogArticleType);
        ExcelUtil<BlogArticleType> util = new ExcelUtil<BlogArticleType>(BlogArticleType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增文章类型
     */
    @RequiresPermissions("blog:type:add")
    @GetMapping("/add/{parent}")
    public String add(@PathVariable("parent") String parent,ModelMap modelMap)
    {
        modelMap.put("type",blogArticleTypeService.selectBlogArticleTypeById(parent));
        return prefix + "/add";
    }
    /**
     * 新增(一级)文章类型
     */
    @RequiresPermissions("blog:type:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/addOne";
    }

    /**
     * 新增保存文章类型
     */
    @RequiresPermissions("blog:type:add")
    @Log(title = "文章类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BlogArticleType blogArticleType)
    {
        return toAjax(blogArticleTypeService.insertBlogArticleType(blogArticleType));
    }

    /**
     * 修改文章类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        BlogArticleType blogArticleType = blogArticleTypeService.selectBlogArticleTypeById(id);
        mmap.put("type", blogArticleType);
        return prefix + "/edit";
    }

    /**
     * 修改保存文章类型
     */
    @RequiresPermissions("blog:type:edit")
    @Log(title = "文章类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BlogArticleType blogArticleType)
    {
        return toAjax(blogArticleTypeService.updateBlogArticleType(blogArticleType));
    }

    /**
     * 删除文章类型
     */
    @RequiresPermissions("blog:type:remove")
    @Log(title = "文章类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(blogArticleTypeService.deleteBlogArticleTypeByIds(ids));
    }
}
